package com.houseliker.activity.calculate;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.houseliker.R;
import com.houseliker.activity.BaseActivity;
import com.houseliker.adapter.calculate.LoanListAdapter;
import com.houseliker.model.LoanDetail;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 公积金贷款、商业贷款 详情页。
 * Created by sunrx on 2015/12/24.
 */
public class LoanDetailActivity extends BaseActivity {

    private TextView tv_amountloan;
    private TextView tv_interest;
    private TextView tv_payment;
    private TextView tv_monthcount;
    private ListView lv_loandetail;

    //贷款月数
    private int monthCount;
    //月利率
    private double monthlyRate;
    //贷款金额
    private double loanAmount;
    //总利息
    private double interestAmount;
    //月本息集合
    private double[] monthlyPrincipalInterests;
    //月均本金
    private double monthlyPrincipal;
    //月均利息
    private double monthlyInterest;

    //贷款类型:等额本息(0)/等额本金(1)
    private int type;

    private List<LoanDetail> lists;

    private DecimalFormat df = new DecimalFormat("0");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.my_loandetail, TYPE_HEADER);
        setHeaderBar("还款详情");

        fetchIntents();

        initViews();

        fillDatas();
    }

    private void fetchIntents() {
        monthCount = getIntent().getIntExtra("monthCount", 0);
        loanAmount = getIntent().getDoubleExtra("loanAmount", 0);
        interestAmount = getIntent().getDoubleExtra("interestAmount", 0);
        monthlyPrincipalInterests = getIntent().getDoubleArrayExtra("monthlyPrincipalInterests");
        monthlyRate = getIntent().getDoubleExtra("monthlyRate", 0);
        type = getIntent().getIntExtra("type", 0);
    }

    private void initViews() {
        tv_amountloan = (TextView) findViewById(R.id.tv_amountloan);
        tv_interest = (TextView) findViewById(R.id.tv_interest);
        tv_payment = (TextView) findViewById(R.id.tv_payment);
        tv_monthcount = (TextView) findViewById(R.id.tv_monthcount);
        lv_loandetail = (ListView) findViewById(R.id.lv_loandetail);
    }

    private void fillDatas() {
        tv_amountloan.setText("贷款总额：" + df.format(loanAmount / 10000) + "万");
        tv_interest.setText("支付利息：" + df.format(interestAmount / 10000) + "万");
        tv_payment.setText("月均还款：" + df.format((loanAmount + interestAmount) / monthCount) + "元");
        tv_monthcount.setText("贷款月数：" + monthCount + "月(" + monthCount / 12 + "年)");
        //
        lists = new ArrayList<>();

        double sum_monthlyPrincipalInterests = 0;
        double remainingLoanAmount = loanAmount;
        int size = monthlyPrincipalInterests.length;
        for (int i = 0; i < size; i++) {
            if (type == 1) {
                //等额本金
                monthlyPrincipal = loanAmount / monthCount;
                monthlyInterest = monthlyPrincipalInterests[i] - monthlyPrincipal;
            } else {
                //等额本息
                monthlyInterest = remainingLoanAmount * monthlyRate;
                monthlyPrincipal = monthlyPrincipalInterests[i] - monthlyInterest;
                remainingLoanAmount -= monthlyPrincipal;
            }
            sum_monthlyPrincipalInterests += monthlyPrincipalInterests[i];
            double surplus = loanAmount + interestAmount - sum_monthlyPrincipalInterests;
            //
            LoanDetail detail = new LoanDetail();
            detail.year = "第" + (i / 12 + 1) + "年";
            detail.month = (i % 12 + 1) + "";
            detail.payment = df.format(monthlyPrincipalInterests[i]) + "";
            detail.payment_principal = df.format(monthlyPrincipal) + "";
            detail.payment_interest = df.format(monthlyInterest) + "";
            detail.surplus = df.format(surplus < 0 ? 0 : surplus) + "";
            detail.state = (i % 12) % 2;
            //
            lists.add(detail);
        }
        LoanListAdapter adapter = new LoanListAdapter(mContext, lists);
        lv_loandetail.setAdapter(adapter);
    }

    @Override
    protected void onLeftButtonClick() {
        super.onLeftButtonClick();
        finish();
    }
}
